package com.yxkj.railservicer.util;

import android.os.Environment;


import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Okhttp的初步封装工具类,{@link OkHttpClient},以后需替换，使用Volley或
 * 另封装
 *
 * @Date 2016/08/26
 */
public class OkhttpUtils {

    //私有的单例OkHttpClient
    private OkHttpClient httpClient;

    //工具类单例
    private static OkhttpUtils singleton;//singleton

    //私有构造 初始化OkHttpClient
    private OkhttpUtils() {
        //缓存地址
        File cachefile = new File(Environment.getExternalStorageDirectory(), "httpCache");
        //缓存空间大小
        long cacheSize = 10 * 1024 * 1024;
        //缓存对象
        Cache cache = new Cache(cachefile, cacheSize);
        httpClient = new OkHttpClient.Builder()
                .cache(cache)//使用自己的缓存地址
                .connectTimeout(5000, TimeUnit.MILLISECONDS)//连接超时时间
                .readTimeout(5000, TimeUnit.MILLISECONDS)//读操作超时时间
                .writeTimeout(5000, TimeUnit.MILLISECONDS)//写操作超时时间
                .build();
    }

    //单例模式
    public static OkhttpUtils getSingleton() {
        if (singleton == null) {
            singleton = new OkhttpUtils();
        }
        return singleton;
    }

    /**
     * @return the only Okhttpclient
     */
    public OkHttpClient getHttpClient() {
        return httpClient;
    }


    /**
     * post请求
     *
     * @param url      地址
     * @param json     json格式参数
     * @param callback 回调
     * @return
     */
    public Call post(String url, String json, Callback callback) {

        /**
         * body 请求体
         * info  key
         * json 参数
         *    json格式的参数包含具体内容 也就是实际的参数
         */
        RequestBody body = new FormBody.Builder().add("info", json).build();
        Request request = new Request.Builder()
                .url(url).post(body).build();
        Call call = httpClient.newCall(request);
        call.enqueue(callback);
        return call;
    }

    /**
     * get请求
     *
     * @param url       地址
     * @param callback  回调
     * @return
     */

    public Call get(String url, Callback callback) {
        Request request = new Request.Builder()
                .url(url).get().build();
        Call call = httpClient.newCall(request);
        call.enqueue(callback);
        return call;
    }

    /**
     * upload file with post method
     *
     * @param url      url
     * @param json     the form data
     * @param file     the file to upload
     * @param callback the callback after the progress completed,may be failed
     * @return the specific call fot the http request,a object of{@link Call},
     * it is userful when you want to cancel this request
     * @DATE 2016/09/13
     */
    public Call uploadFileWithPost(String url, String json, File file, Callback callback) {
        MediaType MEDIA_TYPE = file.getAbsolutePath().endsWith("png") ?
                MediaType.parse("image/png") : MediaType.parse("image/jpeg");
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("info", json)
                .addFormDataPart("file", "file", RequestBody.create(MEDIA_TYPE, file))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = httpClient.newCall(request);
        call.enqueue(callback);
        return call;
    }
}
